#include <QtGui>
#include <iostream>

#include "pcaDialog.h"
#include "../src/data.h"


PCADialog::PCADialog(QWidget *_parent, std::vector<QImage *> _i, std::vector<QString> _names) : QDialog(_parent), originalImages(_i){
    std::vector<Data *> datas;
    QImage *img;
    int m = _i.at(0)->width(), n = _i.at(0)->height();
    for(unsigned int i = 0; i < _i.size(); i++){
        img = _i.at(i);
        qDebug() << i <<": " <<img->format();

        if(m != img->width() || n != img->height()) std::cout <<"Vai dar erro porque são tamanhos diferentes!" <<std::endl;
        datas.push_back(new Data(img->bits(), img->width(), img->height()));
    }

    //mandar calcular logo na classe pca
    //lá vai ter tudo, e ainda vai reconstruir as Data, de acordo com o que se pede
    pca = new PCA(datas);

    testImage = NULL;
//    testImage = new QImage(*(_i.at(0)));

//    filter = new SpatialFilter(new Data(testImage->bits(), testImage->width(), testImage->height()));



    QSplitter *verticalSplitter = new QSplitter(this);
    verticalSplitter->setOrientation(Qt::Vertical);

    preview = new Image(verticalSplitter, testImage);

    QPushButton *closeButton = new QPushButton(tr("Fechar"));
//    QPushButton *applyButton = new QPushButton(tr("Aplicar"));

    connect(closeButton, SIGNAL(clicked()), this, SLOT(notApply()));
//    connect(applyButton, SIGNAL(clicked()), this, SLOT(apply()));




	QLabel* testLabel = new QLabel(tr("test: "));
    QHBoxLayout *contentLayout = new QHBoxLayout;
    contentLayout->addWidget(testLabel);



    page = new PCAPage(_names, verticalSplitter);
    connect(page, SIGNAL(done()), this, SLOT(changeImage()));

    verticalSplitter->addWidget(preview);
    verticalSplitter->addWidget(page);

    resize(600, 400);

    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    buttonsLayout->addStretch(1);
//    buttonsLayout->addWidget(applyButton);
    buttonsLayout->addWidget(closeButton);


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(verticalSplitter);
    mainLayout->addLayout(buttonsLayout);

    setLayout(mainLayout);

    setWindowTitle(tr("Componentes principais"));

    changeImage();
}

PCADialog::~PCADialog(){
}

void PCADialog::apply(){
    QImage *result;
    for(unsigned int i = 0; i < originalImages.size(); i++){
        std::cout << "Retorne algo!" <<std::endl;

        emit done(result);
//        result.push_back();
        //*****
        //de acordo com o que tem que retornar
    }
    emit done();
    accept();
}

void PCADialog::notApply(){
    emit cancel();
    close();
}

void PCADialog::changeImage(){
    //mostra a iamgem tal
    int type = page->getType();
    int image = page->getImage();
    int lambda = page->getLambda() + 1;
//    std::cout <<"change! TYPE: " << type << "  com a imagem: " << image << "  com tantos lambdas: " << lambda<<std::endl;

    delete testImage;

    if(type == 0){//normal
        testImage = new QImage(*originalImages.at(image));
    }else if(type == 3){
        Data *dReconstructed = pca->getData(2, image, lambda);

        QImage *img = originalImages.at(image);
        Data *dOriginal = new Data(img->bits(), img->width(), img->height());

        Data *d = (*dOriginal) - (*dReconstructed);
        testImage = new QImage(d->getData(), d->getWidth(), d->getHeight(), d->getWidth()*4, originalImages.at(0)->format());
    }else if(type == 4){
        Data *dY = pca->getData(1, image, lambda);

        QImage *img = originalImages.at(image);
        Data *dOriginal = new Data(img->bits(), img->width(), img->height());

        Data *d = (*dY) - (*dOriginal);
        testImage = new QImage(d->getData(), d->getWidth(), d->getHeight(), d->getWidth()*4, originalImages.at(0)->format());
    }else{
        Data *d = pca->getData(type, image, lambda);
        testImage = new QImage(d->getData(), d->getWidth(), d->getHeight(), d->getWidth()*4, originalImages.at(0)->format());
    }
    update();
}

void PCADialog::update(){
    if(testImage) preview->update(testImage);
}
